Component({
    properties: {
        // 限制图片数
        maxCount: {
            type: [String, Number],
            value: 9
        },

        // 服务器接口完整URL
        uploadServerUrl: {
            type: String,
            value: 'https://swzfw1.jiaxing.gov.cn/api/common/upload_file2'
        },

        // 上传加号图片URL
        uploadImgUrl: {
            type: String,
            value: 'https://swzfw1.jiaxing.gov.cn/WebImg/jiaxin/upload-file.png'
        },

        // 图片尺寸
        imageSize: {
            type: String,
            value: '160rpx'
        },

        // 图片间距
        imageGap: {
            type: String,
            value: '28rpx'
        }
    },

    data: {
        images: [],
        imagesLength: 0,
    },

    methods: {
        onChooseImage() {
            const MAX_COUNT = this.data.maxCount
            if (this.data.images.length >= MAX_COUNT) {
                wx.showToast({
                    title: `图片不能超过${MAX_COUNT}张`,
                    icon: 'none'
                })
                return
            }
            wx.chooseImage({
                count: MAX_COUNT,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: res => {
                    this.data.imagesLength = 0
                    this.uploadFile(res.tempFilePaths)
                }
            })
        },

        uploadFile(tempFilePaths) {
            wx.uploadFile({
                url: this.data.uploadServerUrl,
                filePath: tempFilePaths[this.data.imagesLength],
                name: 'file',
                success: res => {
                    this.data.imagesLength++
                    this.data.images.push(JSON.parse(res.data).data.vote)
                    this.setData({
                        images: this.data.images
                    })
                    this.triggerEvent('upload', this.data.images)
                    if (this.data.imagesLength < tempFilePaths.length) {
                        if (this.data.images.length >= 9) {
                            this.setData({
                                imagesLength: 0
                            })
                            return
                        }
                        this.uploadFile(tempFilePaths)
                    }
                }
            })
        },

        async onDeleteImage(e) {
            const res = await wx.showModal({
                title: '温馨提示',
                content: '确认删除吗？',
                confirmColor: '#EA1F20'
            })
            if (res.cancel) return
            this.data.images.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                images: this.data.images
            })
            this.triggerEvent('upload', this.data.images)
        },

        onPreviewImage(e) {
            const { urls, current } = e.currentTarget.dataset
            wx.previewImage({
                urls,
                current
            })
        }
    }
})
